package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.ButcherReportsForm;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/17.
 *
 */
@Repository
public class ButcherReportsFormDao extends MyBatisDao<ButcherReportsForm>{

    /**
     * 根据动物种类分类 查询合计
     * @param map 参数
     * @return list
     */
    public List<Map<String , BigDecimal>> findSumGroupByProductType(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findSumGroupByProductType"),map);
    }

    public Float findSumSyncUnqualifiedQuantity(Map<String,Object> map){
        return getSqlSession().selectOne(sqlId("findSumSyncUnqualifiedQuantity"),map);
    }

    public Float syncUnqualifiedQuantitySum(Map<String,Object> map){
        return getSqlSession().selectOne(sqlId("syncUnqualifiedQuantitySum"),map);
    }
    /**
     * 屠宰检疫报表分页查询
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<ButcherReportsForm> butcherReportsFormPaging(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countButcherReportForm"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReportsForm> data = getSqlSession().selectList(this.sqlId("pingButcherReportForm"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 根据动物种类分类 查询合计 (harmless forms)
     * @param map 参数
     * @return list
     */
    public List<Map<String , BigDecimal>> findHarmlessSumGroupByProductType(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findHarmlessSumGroupByProductType"),map);
    }

    /**
     * 屠宰检疫报表分页查询 （harmless forms）
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<ButcherReportsForm> pagingButcherHarmlessForm(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countButcherHarmlessForm"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReportsForm> data = getSqlSession().selectList(this.sqlId("pagingButcherHarmlessForm"), criteria);
            return new Paging<>(total, data);
        }
    }

    public Paging<ButcherReportsForm> pagingCommon(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("count1"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReportsForm> data = getSqlSession().selectList(this.sqlId("paging1"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 屠宰检疫总体情况
     * @param criteria
     * @return
     */
    public List<Map> tzjy(Map criteria){
        return getSqlSession().selectList(this.sqlId("tzjy"),criteria);
    }

    /**
     * 本地入场数
     * @param criteria
     * @return
     */
    public Integer local(Map criteria){
        return getSqlSession().selectOne(this.sqlId("bdtzjy"),criteria);
    }


    public List<Map> localbutcher(Map criteria){
        return getSqlSession().selectList(sqlId("localbutcher"),criteria);
    }

    public ButcherReportsForm findByReportId(Long reportId){
        return getSqlSession().selectOne(sqlId("findByReportId"),reportId);
    }

    /**
     * 下载
     * @param criteria 参数
     * @return List<ButcherReportsForm>
     */
    public List<ButcherReportsForm> findListForWeb(Map<String , Object> criteria){

        return getSqlSession().selectList(sqlId("findListForWeb"),criteria);
    }

}
